﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace RL
{
    /// <summary>
    /// Логика взаимодействия для wPersonSelect.xaml
    /// </summary>
    public partial class wPersonSelect : Window
    {
        private routelistEntities db;

        public routelistEntities DBContext { get { return db; } set {db = value ;} }

        public string FilterString { get; set; }



        public person SelectedPerson
        {
            get { return (person)GetValue(SelectedPersonProperty); }
            set { SetValue(SelectedPersonProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SelectedPerson.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SelectedPersonProperty =
            DependencyProperty.Register("SelectedPerson", typeof(person), typeof(wPersonSelect), new UIPropertyMetadata(null, new PropertyChangedCallback(propPerson)));

        public static void propPerson(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            wPersonSelect item = d as wPersonSelect;
            if (null == item) return;
            person it = e.NewValue as person;
            if (null == it) return;
            if (item.DBContext == null) return;
            item.listPerson.ScrollIntoView(it);
            item.listPerson.SelectedItem = it;
        }


        public int PersonID
        {
            get { return (int)GetValue(PersonIDProperty); }
            set 
            { 
                SetValue(PersonIDProperty, value); 
            }
        }

        // Using a DependencyProperty as the backing store for PersonID.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PersonIDProperty =
            DependencyProperty.Register("PersonID", typeof(int), typeof(wPersonSelect), new UIPropertyMetadata(0, new PropertyChangedCallback(propPersonID)));

        public static void propPersonID(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            wPersonSelect item = d as wPersonSelect;
            if (null == item) return;
            if (null == item.DBContext) return;
            int val = (int)e.NewValue;
            item.SelectedPerson = (from o in item.DBContext.person where o.id == val select o).FirstOrDefault();
        }


        public wPersonSelect()
        {
            InitializeComponent();
            if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this)) return;
            DBContext = new routelistEntities();
        }

        private void ListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            SelectedPerson = listPerson.SelectedItem as person;
            DialogResult = true;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (null == db) return;
            if (null == FilterString) FilterString = "";
            IQueryable<person> query;
            if (string.IsNullOrEmpty(FilterString))
            {
                query = from o in db.person orderby o.fio select o;
            }
            else
            {
                query = from o in db.person where o.fio.StartsWith(FilterString.Trim()) || o.tabel == FilterString.Trim() orderby o.fio select o;
            }
            CollectionViewSource src = (CollectionViewSource)FindResource("sourcePerson");
            src.Source = query;

            if (null != SelectedPerson)
            {
                listPerson.ScrollIntoView(SelectedPerson);
                listPerson.SelectedItem = SelectedPerson;
            }
        }

        private void buttonOK_Click(object sender, RoutedEventArgs e)
        {
            SelectedPerson = listPerson.SelectedItem as person;
            try
            {
                DialogResult = true;
            }
            catch (Exception)
            { }
        }

        private void buttonCancel_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                DialogResult = false;
            }
            catch (Exception)
            { }
        }
    }
}
